package com.example.xungeng2.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.xungeng2.common.Result;
import com.example.xungeng2.pojo.FengxianDate;
import com.example.xungeng2.service.FengxianDateService;
import com.example.xungeng2.unknow.TableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/api/fengxianDate")
public class FengxianController {

    @Autowired
    private FengxianDateService fengxianDateService;

    @Cacheable(value = "fxData",key = "'fengxaindata'")
    @GetMapping("/page")
    public TableMap getPage(Long page,Long limit){
        Page<FengxianDate> fengxianDatePage = fengxianDateService.selectAllPage(page,limit);
        TableMap tableMap = new TableMap();
        tableMap.setData(fengxianDatePage.getRecords());
        tableMap.setCount(fengxianDatePage.getTotal());
        return tableMap;
    }
    @CacheEvict(value = "fxData",allEntries = true)
    @PostMapping("/insert")
    public Result insertinfo(FengxianDate fengxianDate) throws ParseException {
        System.out.println(fengxianDate);
        System.out.println(fengxianDate);
        FengxianDate fengxianDate1 = new FengxianDate();
        fengxianDate1.setHigh(1);
        fengxianDate1.setLow(2);
        fengxianDate1.setZhong(3);
        fengxianDate1.setSum1(6);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFormat.format(new Date());
        Date date = dateFormat.parse(format);
        fengxianDate1.setRiqi(date);
        boolean save = fengxianDateService.save(fengxianDate1);
        if(save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
}